package com.wjf.resourceservice.mapper;

import com.wjf.resourceservice.entity.Playlist;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Date;
import com.wjf.resourceservice.entity.Music;

/**
 * 歌单数据访问层接口
 * 处理歌单相关的数据库操作
 */
@Mapper
public interface PlaylistMapper {
    
    /**
     * 创建新歌单
     * @param playlist 歌单信息
     * @return 影响的行数
     */
    @Insert("INSERT INTO playlist (name, description, cover_url, creator_id, type, status, create_time) " +
            "VALUES (#{name}, #{description}, #{coverUrl}, #{creatorId}, #{type}, #{status}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int createPlaylist(Playlist playlist);
    
    /**
     * 向歌单添加音乐
     * @param playlistId 歌单ID
     * @param musicId 音乐ID
     * @param createTime 创建时间
     * @return 影响的行数
     */
    @Insert("INSERT INTO playlist_music (playlist_id, music_id, create_time) " +
            "VALUES (#{playlistId}, #{musicId}, #{createTime})")
    int addMusicToPlaylist(@Param("playlistId") Long playlistId, 
                          @Param("musicId") Long musicId, 
                          @Param("createTime") Date createTime);
    
    /**
     * 获取用户的歌单列表
     * @param userId 用户ID
     * @return 歌单列表
     */
    @Select("SELECT * FROM playlist WHERE creator_id = #{userId} AND status = 1")
    List<Playlist> getUserPlaylists(Long userId);
    
    /**
     * 获取歌单中的音乐列表
     * @param playlistId 歌单ID
     * @return 音乐列表
     */
    @Select("SELECT m.* FROM music m " +
            "INNER JOIN playlist_music pm ON m.id = pm.music_id " +
            "WHERE pm.playlist_id = #{playlistId} " +
            "ORDER BY pm.sort ASC")
    List<Music> getPlaylistMusics(Long playlistId);
} 